﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class SystemConfigController : UI.BaseController
    {
        public const string ProgrameCode = "SystemConfig";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public SystemConfigController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            var model = BLL.SystemConfig.LoadConfig();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit")]
        public string Index(Model.Config.SystemConfig value)
        {
            if (string.IsNullOrEmpty(value.Mail.Password))
            {
                value.Mail.Password = BLL.SystemConfig.LoadConfig().Mail.Password;
            }
            var ret = BLL.SystemConfig.SaveConifg(value);
            return UI.Utils.ToJsonString(ret);
        }

        [HttpPost]
        [UI.LoginValidate]
        public string TestSMTP(Model.Config.SystemConfig value)
        {
            Common.Result ret = null;
            if (string.IsNullOrEmpty(value.Mail.Password))
            {
                value.Mail.Password = BLL.SystemConfig.LoadConfig().Mail.Password;
            }
            string to=BLL.LoginUser.LoginManager.RealName + ":" + BLL.LoginUser.LoginManager.Email;
            ret = BLL.Mail.MailNotify(value.Mail,to, "SmtpTest", "User", BLL.LoginUser.LoginManager);            
            return UI.Utils.ToJsonString(ret);
        }

    }
}